﻿using System;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using com.fukefu.sql;
using mshtml;
using System.Collections.Generic;

namespace com.fukefu
{
    public partial class HtmlTextBox : UserControl
    {
        IHTMLDocument2 doc;

        public HtmlTextBox()
        {
            InitializeComponent();
            CreateTag(MyTag);
            Init();
        }
        //下拉框选择
        private void CreateTag(ToolStripComboBox tscb)
        {
            SqlServerOperate sql = new SqlServerOperate();
            SqlDataReader re = sql.Query("select * from [全局设置].[dbo].[_mb_标签分类] order by part asc,weights desc");
            while (re.Read())
            {
                tscb.Items.Add("{tag:" + re["word"] + "}");
            }
            tscb.SelectedIndex = tscb.Items.Count > 0 ? 0 : -1;
            re.Close();
            sql.Close();
        }
        //初始化
        private void Init()
        {
            EditStp.Navigate("about:blank");
            //浏览器进入编辑模式
            EditStp.Document.ExecCommand("EditMode", true, null);
            EditStp.Document.ExecCommand("LiveResize", false, null);
            //屏蔽webbrowser右键
            EditStp.Document.ContextMenuShowing += delegate(object sender, HtmlElementEventArgs e)
            {
                e.ReturnValue = false;
            };
            //在DocumentComplete事件（虚函数）中获得浏览器容器的IHTMLDocument2接口(文档接口)
            doc = (IHTMLDocument2)this.EditStp.Document.DomDocument;
            SetHtml("");
        }
        //插入tag
        private void InsertTag_Click(object sender, EventArgs e)
        {
            IHTMLTxtRange range = doc.selection.createRange() as IHTMLTxtRange;
            range.pasteHTML("<font color=red>" + MyTag.Text + "</font>");
        }
        //加粗
        private void Bold_Click(object sender, EventArgs e)
        {
            EditStp.Document.ExecCommand("Bold", false, null);
        }
        //斜体
        private void Italic_Click(object sender, EventArgs e)
        {
            EditStp.Document.ExecCommand("Italic", false, null);
        }
        //下划线
        private void Underline_Click(object sender, EventArgs e)
        {
            EditStp.Document.ExecCommand("Underline", false, null);
        }
        //文字颜色
        private void ForeColor_Click(object sender, EventArgs e)
        {
            ColorDialog dg = new ColorDialog();
            if (dg.ShowDialog() == DialogResult.OK)
            {
                EditStp.Document.ExecCommand("ForeColor", false, dg.Color);
            }
        }
        //插入开头空格
        private void Indent_Click(object sender, EventArgs e)
        {
            IHTMLTxtRange range = doc.selection.createRange() as IHTMLTxtRange;
            range.pasteHTML("　　");
        }
        //是否加粗
        private bool IsBold()
        {
            return doc.queryCommandState("Bold");
        }
        //是否斜体
        private bool IsItalic()
        {
            return doc.queryCommandState("Italic");
        }
        //是否下划线
        private bool IsUnderline()
        {
            return doc.queryCommandState("Underline");
        }
        //取得网页代码
        public string GetHtml()
        {
            string html = EditStp.DocumentText;
            html = Regex.Match(html, "<BODY>\\s*([\\d\\D]*)</BODY></HTML>", RegexOptions.IgnoreCase).Groups[1].Value;
            html = Regex.Replace(html, "<font[\\d\\D]*?>({tag:[^}]+})</font>", "$1", RegexOptions.IgnoreCase);
            return html;
        }
        //设置网页代码
        public void SetHtml(string html)
        {
            EditStp.Navigate("about:blank");
            //这两行屏蔽弹出的对话框
            EditStp.AllowNavigation = true;
            EditStp.Document.OpenNew(true);
            if (html.Equals(""))
            {
                html = "<P>　　</P>";
            }
            html = MyFunction.ToHtml(MyFunction.ToText(html, 1));
            html = Regex.Replace(html, "{tag:[^}]+}", "<font color=red>$0</font>");
            EditStp.DocumentText = "<!DOCTYPE html><html><head><style type=text/css>*{margin:0;}</style></head><body>" + html + "</body></html>";
        }
        //计时器
        private void timer1_Tick(object sender, EventArgs e)
        {
            Bold.Checked = IsBold();
            Italic.Checked = IsItalic();
            Underline.Checked = IsUnderline();
        }
        //删除浏览器内容
        private void Delete_Click(object sender, EventArgs e)
        {
            SetHtml("");
        }

        //一键格式化
        private void formatHtml_Click(object sender, EventArgs e)
        {
            SetHtml(MyFunction.ToHtml(MyFunction.ToText(GetHtml(), 1)));
        }

        private void LookCodeButton_Click(object sender, EventArgs e)
        {
            MessageBox.Show(GetHtml(), "代码");
        }
        //改变字体大小
        private void FontToolStripComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (Regex.IsMatch(this.FontToolStripComboBox.Text, "[0-9]+"))
            {
                EditStp.Document.ExecCommand("FontSize", false, this.FontToolStripComboBox.Text);
            }
        }
    }
}
